// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

type DataMsg struct {
	Prefix
	data []byte
}

type CommentMsg struct {
	Prefix
	comment string
}

type Serializer struct {
	dataCh    chan DataMsg
	commentCh chan CommentMsg
}

func (s *Serializer) Run(changed chan Prefix, dout chan []byte, cout chan string) {
	p := Prefix{-1, '?'} // to force change on first input
	for {
		select {
		case d := <-s.dataCh:
			if !p.equal(d.Prefix) {
				p = d.Prefix
				changed <- p
			}
			dout <- d.data
		case c := <-s.commentCh:
			// comment always forces change
			p = c.Prefix
			changed <- p
			cout <- c.comment
		}
	}
}

func NewSerializer() *Serializer {
	return &Serializer{make(chan DataMsg), make(chan CommentMsg)}
}
